﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CanDichVu.Db;
using CanDichVu.Service.Iservice;
using CanDichVu.Db.Entity;

namespace CanDichVu.Service.Vervices
{
    public class CateSer : ICate
    {
        private readonly EntitiesCanDichVu dbContext = new EntitiesCanDichVu();

        public int addorUpdate(Db.ChuyenMuc entity)
        {
            try
            {
                dbContext.Entry(entity).State = entity.Id == 0 ? System.Data.EntityState.Added : System.Data.EntityState.Modified;
                dbContext.SaveChanges();
                return entity.Id;
            }
            catch
            {
                return 0;
            }
        }

        public List<ChuyenMuc> GetAllCategories()
        {
            try
            {
                return dbContext.ChuyenMucs.OrderBy(p => p.stt).ToList();
            }
            catch
            {
                return null;
            }
        }

        public List<ChuyenMuc> GetAllSetFooter()
        {
            try
            {
                return dbContext.ChuyenMucs.Where(p => p.set_footer == true).OrderBy(p => p.stt).ToList();
            }
            catch
            {
                return null;
            }
        }

        public List<Db.ChuyenMuc> getListByParent(int cateid)
        {
            try
            {
                return dbContext.ChuyenMucs.OrderBy(p => p.stt).Where(p => p.ParentId == cateid).ToList();
            }
            catch
            {
                return null;
            }
        }

        public bool detele(int id)
        {
            try
            {
                var _entity = GetById(id);
                if (_entity.ParentId == 0)
                {
                    var _entityList = getListByParent(_entity.Id);
                    if (_entityList.Count() > 0)
                    {
                        foreach (var item in _entityList)
                        {
                            dbContext.ChuyenMucs.Remove(item);
                            dbContext.SaveChanges();
                        }
                    }
                }
                dbContext.ChuyenMucs.Remove(_entity);
                dbContext.SaveChanges();
                return true;
            }
            catch { return false; }
        }


        public ChuyenMuc GetById(int id)
        {
            try
            {
                return dbContext.ChuyenMucs.Where(p => p.Id == id).First();
            }
            catch { return null; }
        }

        public List<ListEntityDorpList> ListCateAllDorp(int nhomcha)
        {
            List<ListEntityDorpList> ListRetuen = new List<ListEntityDorpList>();

            var listArrayCateId = dbContext.ChuyenMucs.Where(p => p.ParentId == nhomcha).ToList();

            foreach (var i in listArrayCateId)
            {
                ListEntityDorpList ml = new ListEntityDorpList();
                ml.id = i.Id;
                ml.text = i.TenChuyenMuc;
                ml.children = new List<ListEntityDorpList>();
                var _list = ListCateAllDorp(i.Id);
                ml.children.AddRange(_list);

                ListRetuen.Add(ml);

            }

            return ListRetuen;
        }
    }
}
